package com.example.financeapi.service.impl;

import com.example.financeapi.entity.FSecuritiesAccount;
import com.example.financeapi.entity.FSecuritiesCompany;
import com.example.financeapi.mapper.FSecuritiesAccountMapper;
import com.example.financeapi.mapper.FSecuritiesCompanyMapper;
import com.example.financeapi.service.FSecuritiesAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 证券账户表(FSecuritiesAccount)表服务实现类
 *
 * @author Alvin
 * @since 2020-03-31 12:05:09
 */
@Service
public class FSecuritiesAccountServiceImpl implements FSecuritiesAccountService {
    @Autowired
    private FSecuritiesAccountMapper fSecuritiesAccountMapper;
    @Autowired
    private FSecuritiesCompanyMapper fSecuritiesCompanyMapper;

    /**
     * 获取账号列表
     * @param paramMap
     * @return
     */
    @Override
    public List<FSecuritiesAccount> getAccountList(Map<String, Object> paramMap) {
        return fSecuritiesAccountMapper.getAccountList(paramMap);
    }

    /**
     * 查询单个账号信息
     * @param id
     * @return
     */
    @Override
    public FSecuritiesAccount queryById(String id) {
        return this.fSecuritiesAccountMapper.queryById(id);
    }

    /**
     * 添加账号信息
     * @param fSecuritiesAccount
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, readOnly = false, rollbackFor = Exception.class)
    public int insert(FSecuritiesAccount fSecuritiesAccount){
        Map<String,Object> paramMap =new HashMap<>();
        paramMap.put("ascriptionPlatform",fSecuritiesAccount.getAscriptionPlatform().trim());
        paramMap.put("customeCode",fSecuritiesAccount.getCustomeCode().trim());
        List<FSecuritiesAccount> resultListTwo =fSecuritiesAccountMapper.getAccountList(paramMap);
        if(resultListTwo.size()>0){
            return 0;
        }
        return fSecuritiesAccountMapper.insert(fSecuritiesAccount);
    }

    /**
     * 获取证券列表
     * @param paramMap
     * @return
     */
    @Override
    public List<FSecuritiesCompany> getSecuritiesCompanyList(Map<String, Object> paramMap) {
        return fSecuritiesCompanyMapper.getSecuritiesCompanyList(paramMap);
    }

    /**
     * 修改数据
     * @param fSecuritiesAccount 实例对象
     * @return 实例对象
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, readOnly = false, rollbackFor = Exception.class)
    public int update(FSecuritiesAccount fSecuritiesAccount) throws SQLException {
        return fSecuritiesAccountMapper.update(fSecuritiesAccount);
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public int deleteById(String id) {
        return this.fSecuritiesAccountMapper.deleteById(id);
    }
}